"""
FileName：base_test.py
Description：
Author：gaojiahui
Time：2025/8/17 18:10
Website：https://gitee.com/gao-jiahui
"""
from selenium import webdriver

from utils.exceptions import handle_exceptions
from utils.logger import logger
from utils.retry import retry_on_failure

"""框架核心基类"""
class BaseTest:
    def __init__(self):
        self.driver = None
        logger.info("测试初始化")

    def setup(self):
        """测试前置设置"""
        logger.info("启动浏览器")
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()

    @handle_exceptions
    @retry_on_failure(max_attempts=2, delay=1, backoff=2)
    def perform_action(self, locator, action="click", value=None):
        """执行元素操作"""
        logger.debug(f"执行操作: {action} on {locator}")
        element = self.driver.find_element(*locator)

        if action == "click":
            element.click()
        elif action == "input":
            element.clear()
            element.send_keys(value)
        elif action == "get_text":
            return element.text
        logger.info(f"操作成功: {action} on {locator}")

    @handle_exceptions
    def assert_condition(self,condition,message):
        """带日志记录的断言"""
        logger.debug(f"断言检查: {message}")
        assert condition, message
        logger.info(f"断言通过: {message}")

    def teardown(self):
        """测试清理"""
        logger.info("关闭浏览器")
        if self.driver:
            self.driver.quit()